from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
# Create your views here.
from django.template.loader import render_to_string

from django.urls import reverse
from django.views.generic import TemplateView


def hello_world(request):
    from django.urls import reverse
    print(reverse('hello_world')) #打印URL路径
    return HttpResponse('hello world')
def hello_china(request):
    raise
    from django.urls import reverse
    print(reverse('hello_china'))
    return HttpResponse('hello china')
def hello_html(request):
    """响应HTML内容 """
    username='张三'
    html="""
    <html>
        <body>
            <h1 style="color:#f00">
            hello {{html}}
            </h1>
        </body>
    </html>
    """.replace('{{html}}',username)
    return HttpResponse(html)
def article_list(request,month):
    """
    param month:今年某一个月的文章列表
    """
    return HttpResponse('article:{}'.format(month))
def search(request):
    """GET参数的获取"""
    name=request.GET.get('name','')   #获取'name'，如果没有，返回空值'',name需要自己在URL路径中输入：?name=xxx
    print(name)
    return HttpResponse('查询成功')

def render_str(request):
    """ render_to_string函数的使用"""
    templ_name='index.html'
    html=render_to_string(template_name=templ_name) # template_name模板名称
    return HttpResponse(html)
def render_html(request):
    """ render函数的使用"""
    return render(request,'index.html')   # 'index.html'是文件的相对路径

def http_request(request):
    """ 请求练习"""
    # 1、请求方式
    print(request.method)
    # 2、请求头的信息
    headers=request.META
    print(headers)
    ua=request.META.get('HTTP_USER_AGENT',None)  #获取访问者电脑信息浏览器
    print(ua)
    print(request.headers)
    print(request.headers['User-Agent'])
    print(request.headers['user-agent']) # 大小写都行
    # 3、获取请求参数
    name=request.GET.get('name','')
    print(name)
    return HttpResponse('响应')
from django.http import JsonResponse
from django.http import FileResponse
def http_response(request):
    """ 响应练习"""
    resp=HttpResponse('响应内容',status=201)  # 状态码 审查元素 ctrl+shift+I
    resp.status_code=204 # 设置状态码
    print(resp.status_code)# 查看状态码
    """ Json响应练习"""
    user_info={
        'name':'张三',
        'age':34
    }
    """ 图片响应"""
    #reponse = FileResponse(open('myfile.png', 'rb'))
    return JsonResponse(user_info,json_dumps_params={'ensure_ascii':False} )
    #resp,JsonResponse(user_info),reponse
def no_data_404(request):
    """ 404页面"""
    return HttpResponse('404')
def article_detail(request,article_id):
    """ 文章详情，ID是从1000开始的整数
    ：param article_id:文章ID
    """
    if article_id <1000 :
        #return HttpResponseRedirect(reverse('no_data_404'))   # 重定向到404页面
        from django.shortcuts import redirect
        #return HttpResponseRedirect(reverse('no_data_404'))  #reverse('no_data_404') '/hello/404/'   或者任何地址  推荐√
        return redirect('/hello/404/')
    return HttpResponse('文章{}的内容'.format(article_id))
    # 错 any数字 都是article:999 article:1000   3-9

class YT(object):
    def display(self):
        return '我是杨婷'
def index (request):
    username='张三'
    age=22
    img_url='/media/images/yt.jpg' #D:/python/Development/MyProject
    list_users=[
        {'name':'张三','age': 34},
        {'name': '李四', 'age': 22},
    ]
    yt=YT()
    return render(request,'index.html',{
        'username':username,
        'age':age,
        'img_url':img_url,
        'list_users':list_users,
        'yt':yt
    })

def tag(request):
    """DTL的标签练习"""
    list_users = [
        {'name': '张三', 'age': 34},
        {'name': '李四', 'age': 22},
        {'name': '张三2', 'age': 34},
        {'name': '李四2', 'age': 22},
    ]
    #list_users=[]
    return render(request,'tag.html',{
        'list_users':list_users
    })

class HomeView(TemplateView):
    template_name = 'home.html'

def test_filter(request):
    """过滤器的使用"""
    name='model'
    list_user=[]
    user_info=None
    pi=3.1415
    from datetime import datetime
    my_date=datetime(2050,9,28)
    html='<h3>欢迎学习XXXXXXX欢迎学习，祝大家学习愉快</h3>'
    return render(request,'test_filter.html',{
        'name':name,
        'list_user':list_user,
        'user_info':user_info,
        'pi':pi,
        'my_date':my_date,
        'html':html
    })

def mine_filter(request):
    """自定义过滤器"""
    uname='张三的年龄'
    return render(request,'mine_filter.html',{
        'uname':uname
    })